#include <stdio.h>
#include <math.h>

// static int env_pt1_dbm = -10;
float      pt1_dbm     = -10.45;
static int env_pt1_dac = 300;
static int env_pt1_adc = 260;

// static int env_pt2_dbm = -3;
float      pt2_dbm     = -8.75;
static int env_pt2_dac = 400;
static int env_pt2_adc = 417;

float ka, ca;
float kd, cd;
struct line_kc
{
    float k;
    float c;
};
struct line_kc fun_line(float x1, float y1, float x2, float y2)
{
    struct line_kc cal;
    cal.k = (float)(y2 - y1) / (x2 - x1);
    cal.c = y2 - cal.k * x2;
    printf("k %f c %f\r\n", cal.k, cal.c);
    return cal;
}

float dbm_dac(float dbm, struct line_kc *cal)
{
    // float pb = pow(10, pt1_dbm / 10);
    float y, x = pow(10, dbm / 10);

    // float det = pt / pb;

    y = cal->k * x + cal->c;
    printf("%f %f\r\n", x, y);
    // printf("%f %f det %f\r\n", pb, pt, det);
    // printf("dac %f adc %f\r\n", det * env_pt1_dac, det * env_pt1_adc);

    // pb  = pow(10, pt2_dbm / 10);
    // det = pt / pb;
    // printf("%f %f det %f\r\n", pb, pt, det);
    // printf("dac %f adc %f\r\n", det * env_pt2_dac, det * env_pt2_adc);
    return y;
}


void main()
{
    struct line_kc lpd, lpa;

    lpd = fun_line(
	    pow(10, pt1_dbm / 10),
	    env_pt1_dac,
	    pow(10, pt2_dbm / 10),
	    env_pt2_dac);
    lpa = fun_line(
	    pow(10, pt1_dbm / 10),
	    env_pt1_adc,
	    pow(10, pt2_dbm / 10),
	    env_pt2_adc);
    // lpa = fun_line(
    // 		pt1_dbm,
    // 		env_pt1_adc,
    // 		pt2_dbm,
    // 		env_pt2_adc);
    float dac, adc;
    // printf("k %f c %f\r\n", lpd.k, lpd.c);
    // printf("k %f c %f\r\n", lpa.k, lpa.c);
    dac = dbm_dac(-0, &lpd);
    adc = dbm_dac(-0, &lpa);
    printf("da %f adc %f\r\n", dac, adc);
}